package com.AEWMS.Servlet.util;

import com.AEWMS.Dao.Parent;
import com.AEWMS.Dao.Student;
import com.AEWMS.Service.ParentService;
import com.AEWMS.Service.StudentService;
import com.AEWMS.Utlis.MailSender;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ths
 * @create 2021/4/24 2:14
 */
@WebServlet(name = "MailSenderScoreServlet", urlPatterns = "/MailSenderScoreServlet")
public class MailSenderScoreServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String emailTitle = "学业预警系统成绩预警信息";
        String userId = request.getParameter("userid");
        String userType = request.getParameter("userType");
        String attend = request.getParameter("attend");
        //学生
        if(userType.equals("student")){
            StudentService ss = new StudentService();
            Student s = ss.findStuNotViewById(userId);
            System.out.println(s);
            ss.closeCon();
            if (s.getStudentTel()==null){
                response.getWriter().println(JSON.toJSONString("该学生未注册邮箱"));
                return;
            }
            String content = "亲爱的 "+s.getStudentRealName() + ":\n\n" +
                    "    您的成绩预警等级为"+attend+"，请重视。\n\n" + "学业预警系统";
            //调用静态方法
            MailSender.sendMail(emailTitle, s.getStudentTel(), content);
            response.getWriter().println(JSON.toJSONString("邮件发送成功"));
        }
        //家长
        if (userType.equals("parent")){
            ParentService ps = new ParentService();
            Parent p = ps.findChildById(userId);
            if (p.getParentTel()==null){
                response.getWriter().println("该家长未注册邮箱");
                return;
            }
            String content = "尊敬的家长，你的孩子，学号 "+p.getStudentNumber() + ":\n\n" +
                    "    成绩预警等级为"+attend+"，请重视。\n\n" + "学业预警系统";
            //调用静态方法
            MailSender.sendMail(emailTitle, p.getParentTel(), content);
            response.getWriter().println(JSON.toJSONString("邮件发送成功"));
            ps.closeCon();
        }
    }
}
